{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 公有云部署AppBuilderSDK\n",
    "**注意⚠️：部署上云功能目前处于内测阶段，使用过程中有任何问题，欢迎提issue或微信群反馈～**\n",
    "\n",
    "\n",
    "## 目标\n",
    "一键将自己的服务部署到公有云上，目前支持百度智能云。部署后可以自动生成公网ip，无需额外配置。具体可以用来解决以下问题：\n",
    "* 在百度云部署的WebService，可以引入到AppBuilder工作流中的API节点，形成应用中嵌套应用，构建能够解决复杂业务问题的工作流。\n",
    "* 可将自己本地复杂的服务、组件、模型等，方便快捷地上云，并与AppBuilder形成联动。\n",
    "* 构建自己的公网对话服务。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 部署\n",
    "### 前置操作:\n",
    "* 在百度智能云完成实名认证，并开通bos服务：\n",
    "  * https://cloud.baidu.com/doc/BOS/s/Jk4xttg03#%E5%BC%80%E9%80%9Abos%E6%9C%8D%E5%8A%A1\n",
    "* 您需要在钱包中至少充值100元，用于bcc服务、bos服务费用\n",
    "  * 云服务费用标准：https://cloud.baidu.com/product-price/bcc.html\n",
    "  * 默认配置使用最便宜的bcc.e1.c2m2，并且使用按量付费\n",
    "  * bos存储服务费用标准：https://cloud.baidu.com/product-price/bos.html\n",
    "* 通过如下步骤获得您的AK/SK信息：\n",
    "  * https://cloud.baidu.com/doc/Reference/s/9jwvz2egb\n",
    "* 部署的AppBuilder >= 0.9.0\n",
    "\n",
    "### 部署流程：\n",
    "通过下面三步执行部署：\n",
    "* Step1 以下面的yaml文件为模板创建config.yaml，完善并创建配置文件\n",
    "  * 最小修改范围：ak、sk、admin_pass、run_cmd、local_dir、APPBUILDER_TOKEN\n",
    "```yaml\n",
    "bce_config:\n",
    "    host: \"http://bcc.bj.baidubce.com\"\n",
    "    bos_host: \"bj.bcebos.com\"\n",
    "    # 前置操作获取的ak、sk\n",
    "    ak: \"\"\n",
    "    sk: \"\"\n",
    "    spec: \"bcc.e1.c2m2\"\n",
    "    root_disk_size_in_gb: 20\n",
    "    # 服务器密码，根据实际使用设置\n",
    "    admin_pass: \"\"\n",
    "    security_group_id: \"\"\n",
    "    zone_name: cn-bj-d\n",
    "\n",
    "appbuilder_config:\n",
    "    # 本地用来打包的代码路径，根据实际使用修改\n",
    "    local_dir: \"./sample\"\n",
    "    workspace: \"/home/work/appbuilder\"\n",
    "\n",
    "    # 服务的运行命令，根据实际使用修改\n",
    "    run_cmd: \"python3 sample.py\"\n",
    "\n",
    "env:\n",
    "    APPBUILDER_LOGLEVEL: debug\n",
    "    APPBUILDER_TOKEN: \"\"\n",
    "```\n",
    "* Step2 编写自己的服务运行代码（以组件服务为例）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# 创建配置中的local_dir目录\n",
    "mkdir sample\n",
    "touch sample/sample.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# 编写服务运行代码\n",
    "# sample/sample.py\n",
    "import appbuilder\n",
    "\n",
    "component = appbuilder.Playground(prompt_template=\"{query}\", model=\"ERNIE-Bot\")\n",
    "agent = appbuilder.AgentRuntime(component=component)\n",
    "agent.serve(port=8091)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Step3 执行部署"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# ./config.yaml为第一步创建的config.yaml文件\n",
    "appbuilder_bce_deploy --conf ./config.yaml\n",
    "\n",
    "# 执行后取日志\"deployment finished! public ip:\"后的ip为部署实例的公网ip \n",
    "# 若部署结果不符合预期，使用自己配置的\"admin_pass\"登陆服务器进行调试，具体参考文档：https://cloud.baidu.com/doc/BCC/s/Hkbblll70"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 示例：Component服务，接入AppBuilder工作流\n",
    "示例代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "import appbuilder\n",
    "\n",
    "component = appbuilder.Playground(prompt_template=\"{query}\", model=\"ERNIE-Bot\")\n",
    "agent = appbuilder.AgentRuntime(component=component)\n",
    "agent.serve(port=8091)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "示例请求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "# public_ip为部署获取的实例公网IP\n",
    "curl --location 'http://{public_ip}:8091/chat' \\\n",
    "--header 'Content-Type: application/json' \\\n",
    "--data '{\n",
    "    \"message\": \"海淀区的面积是多少\",\n",
    "    \"stream\": false\n",
    "}'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过API节点接入Appbuilder工作流，用于创建组件\n",
    "* 操作参考：https://cloud.baidu.com/doc/AppBuilder/s/glv0f48qe\n",
    "* API接入调试成功\n",
    "\n",
    "<img src=\"../app_builder_resources/component_api_debug.png\" alt=\"drawing\" width=\"800\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 示例：随时随地的对话服务 \n",
    "**⚠️示例仅提供demo，实际业务需增加鉴权等能力**\n",
    "\n",
    "\n",
    "示例代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "import appbuilder\n",
    "\n",
    "\n",
    "component = appbuilder.Playground(prompt_template=\"{query}\", model=\"ERNIE-Bot\")\n",
    "\n",
    "agent = appbuilder.AgentRuntime(component=component)\n",
    "agent.chainlit_demo(port=8091)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "示例效果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"../app_builder_resources/deploy_chainlit.png\" alt=\"drawing\" width=\"300\"/>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
